import Taro ,{ Component } from '@tarojs/taro'
import { View , Swiper , SwiperItem , Image , Ad} from '@tarojs/components'

import './storeList.scss'
import BasePage from "../basePage";
import ListEmptyView from '../../component/LiseEmptyView/listEmptyView.js'
import StoreItem from '../../component/StoreItem/storeItem.js'

import emptyIcon from '../../image/ic_empty.png'
import DataManager from "../../utils/DataManager";
import {base_url} from "../../network/config";
import {COMMON_CONST, STORAGE_KEY} from "../../utils/const";

let mPageNum = 1;
let latitude = '';
let longitude = '';

export default class storeList extends BasePage {
  config = {
    navigationBarTitleText: ''
  }

  constructor() {
    super(...arguments)
    this.state = {
      storeList: [],
      showAd:true,
    }
    latitude = Taro.getStorageSync(STORAGE_KEY.LATITUDE);
    longitude = Taro.getStorageSync(STORAGE_KEY.LONGITUDE);
  }

  componentDidMount(){
    Taro.setNavigationBarTitle({
      title: DataManager.getAddressName() + '商家'
    })
    this.loadStoreList(1)
  }

  onPullDownRefresh(){
    mPageNum = 1;
    this.loadStoreList(mPageNum);
  }

  onReachBottom(){
    mPageNum ++;
    this.loadStoreList(mPageNum)
  }

  loadStoreList(pageNo){
    console.log('loadpage------------' + pageNo)

    let _this = this;
    let url = base_url + 'stateless/business/businessList'
    Taro.request({
      url:url,
      data: {
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'c',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        pageNo: pageNo,
        pageSize: COMMON_CONST.LIST_SIZE,
        latitude:latitude,
        longitude:longitude,
      },
      method: 'GET',
      success(res) {
        if (!res.data || !res.data.data || !res.data.data.list || res.data.data.list.length < 1){
          if (mPageNum > 1){
            mPageNum --;
          }
        }

        if (!res || !res.data || !res.data.data || !res.data.data.list){
          return
        }

        let shops = [];
        if (pageNo == 1){
          shops = res.data.data.list;
        } else {
          shops = _this.state.storeList.concat(res.data.data.list)
        }

        _this.setState({
          storeList: shops
        })
      } ,
      fail: function () {
        if (mPageNum > 1){
          mPageNum --;
        }
      },
      complete:function(){
        Taro.stopPullDownRefresh();
      }
    })
  }

  onAdError = () =>{
    this.setState({
      showAd : false
    })
  }

  onAdLoad = () =>{
    this.setState({
      showAd:true
    })
  }

  render(){
    return(
      <View className='store-list-container'>
        {DataManager.getHasShowAd() &&
        <Ad unitId='adunit-39adab38d2f9a40b' onError={this.onAdError.bind(this)} onLoad={this.onAdLoad.bind(this)}/>
        }
        {this.state.showAd &&
        <View className='store-list-divider'/>
        }
        <View>
          {this.state.storeList.length > 0 ? this.state.storeList.map(item => {
              return (<StoreItem storeData={item}></StoreItem>)
            })
            :
            <ListEmptyView emptyText='暂无数据' emptyImage={emptyIcon}/>
          }
        </View>
      </View>
    )
  }


}




